package planner;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import planner.domain.Domain;
import planner.goals.Activity;
import planner.goals.ActivityLoader;
import planner.plan.Plan;
import planner.plan.PlanOutputter;



public class Tester {
	private Plan p;
	private ArrayList<String> g;
	private POPlanner poplan;
	private Activity currentActivity;
	private ArrayList<String> iProp;
		
	public static void main(String[] args) {
	
		Tester t = new Tester();		
		t.makePlanFile("../ATLSandbox/models/ApplGoals.xml", "../ATLSandbox/models/SearchSpace.xml");
	}
	
	public void makePlanFile(String applGoalsFile, String domainFile) {
				
		ActivityLoader al = new ActivityLoader(applGoalsFile);
		Domain d = new Domain(domainFile);	
		
		int planNumber = 0;
		PlanOutputter po = new PlanOutputter("PlanFile.xml");		 
		
		g = new ArrayList<String>();	
		
		while(al.hasNextActivity()){
			
			currentActivity = al.getNextActivity();
			
			p = new Plan();
			poplan = new POPlanner(d);
			iProp = new ArrayList<String>();
			
//			System.out.println();
//			System.out.println("Initial World State");
//			System.out.println(iProp);			
			
			while(currentActivity.hasNextGoal()){				
				g.clear();
				g.add(currentActivity.getNextGoal());							
				System.out.println("Partial Order Plan -- target : " + g);

				try{

					Plan aux = poplan.makePlan(iProp, g);
					aux.printPlan();
					p.mergeNextPlan(aux);
					
					iProp.clear();
					iProp.addAll(poplan.getFinalWorldState());
//					System.out.println("----------------Partial Merged Plan----------------");
//					p.printPlan();
					
				} catch (Exception e) {					
					e.printStackTrace();
				}
			}
			System.out.println("----------------Complete Plan----------------");
			p.printPlan();
			po.addPlan(p, currentActivity.getName());
									
		}

		try {
			po.printFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}
